<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

// user_providers
class udistributors extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->admin_navigation->parent_active('users');
        $this->load->model('users/users_model');
        $this->load->model('categories/Mcareers_model');
        $this->load->model('categories/Mprovinces_model');
        $this->load->model('categories/Mlevels_model');
        $this->load->model('categories/Mstatus_model');
        $this->load->model('categories/Mfinancials_model');

        //$this->output->enable_profiler(TRUE);
    }
    
    function index()
    {
        $columns = array(
            array(
                'name' => 'ID User',
                'type' => 'id',
                'filter' => 'id_user',
                'width' => '60px',
                'index' => 'id_user',
                'sort_column' => true
            ),
            array(
                'name' => 'Name',
                'type' => 'text',
                'filter' => 'name',
                'index' => 'name',
                'sort_column' => true
            ),
            array(
                'name' => 'User name',
                'type' => 'text',
                'filter' => 'username',
                'index' => 'username',
                'sort_column' => true
            ),
            array(
				'name'=>__('Ngành nghề'),
				'type'=>'select',
				'index'=>'career_name',
				'filter'=>'id_careers',
				'options'=>$this->Mcareers_model->get_objects_options()
			),
			array(
				'name'=>__('Tỉnh thành'),
				'type'=>'select',
				'index'=>'province_name',
				'filter'=>'id_provinces',
				'options'=>$this->Mprovinces_model->get_objects_options()
			),
			array(
				'name'=>__('Nhóm người dùng'),
				'type'=>'select',
				'index'=>'level_name',
				'filter'=>'id_levels',
				'options'=>$this->Mlevels_model->get_objects_options()
			),
			array(
				'name'=>'Ngày tạo',
				'type'=>'date',
				'width' => '80px',
				'index'=>'created',
			),
            array(
                'name' => 'Trạng thái',
                'type' => 'publish',
                'filter' => 'status',
                'index' => 'status',
                'options' => array(
                    '0' => 'Tắt',
                    '1' => 'Bật'
                )
            )
        );

        $this->grid->columns($columns);
        $this->grid->datasource('users/users_model', 'get_objects', array('id_objects' => 2));
        
        $this->grid->set_title('Quản lý thông tin nhà phân phối');
        $this->grid->add_button('Thêm mới', admin_url('*/udistributors/add'), 'add');
        $this->grid->set_row_action('edit');
        
        $this->grid->total_rows($this->users_model->get_objects_count());
        $this->grid->add_mass_action('delete', array(
            'label' => 'Delete',
            'url' => admin_url('*/udistributors/delete'),
            'confirm' => 'Are you sure?'
        ));
        $this->grid->add_mass_action('public', array(
            'label' => 'Public',
            'url' => admin_url('*/udistributors/status_public')
        ));
        $this->grid->add_mass_action('unpublic', array(
            'label' => 'Unpublic',
            'url' => admin_url('*/udistributors/status_unpublic')
        ));
        
        if ($this->input->is_ajax()) {
            $this->output->set_output($this->grid->display());
        } else {
            $this->load->view('admin/grid');
        }
    }
    
    function edit()
    {
        $id = $this->input->get_param('id', false);
        if ($id) {
            $data = $this->users_model->get_object_by_id($id);
            $this->form->set_values($data);
        }
        $this->form->set_form_attributes(array(
            'id' => 'form',
            'action' => admin_url('*/udistributors/edit_post', array(
                'id' => $id,
            )),
            'enctype'=>'multipart/form-data',
        ));
        
        $this->form->add_filedset('ganeral1', array(
            'legend' => 'Tài khoản'
        ));
        
        $this->form->add_filed('username', 'text', array(
            'label' => $this->__('user name'),
            'name' => 'username',
            'required' => true
        ));
        
        $this->form->add_filed('password', 'password', array(
            'label' => $this->__('password'),
            'name' => 'password',
        ));
        
        $this->form->add_filed('password_again', 'password', array(
            'label' => $this->__('password Again'),
            'name' => 'password_again',
        ));

        $this->form->add_filed('email', 'text', array(
            'label' => $this->__('Email'),
            'name' => 'email',
            'required' => true
        ));

        $this->form->add_filed('is_active','select',array(
            'label'=>'Active',
            'name'=>'is_active',
            'values'=>array('Tắt','Bật')
        ));

        $this->form->add_filed('id_levels','select',array(
            'label'=> 'Nhóm người dùng',
            'name'=> 'id_levels',
            'values'=>$this->Mlevels_model->get_objects_options(),
            'required' => true,
        ));

        $this->form->add_filed('status','select',array(
            'label'=>'Status',
            'name'=>'status',
            'values'=>array('Tắt','Bật')
        ));

        /////////////////////////////////
        $this->form->add_filedset('ganeral2', array(
            'legend' => 'Hình ảnh'
        ));

        $this->form->add_filed('image1','image',array(
            'label'=>'Image Slide 1',
            'name'=>'slide1',
        ));

        $this->form->add_filed('image2','image',array(
            'label'=>'Image Slide 2',
            'name'=>'slide2',
        ));

        $this->form->add_filed('avatar','image',array(
            'label'=>'Image Avatar',
            'name'=>'avatar',
        ));
        
        ////////////////////////////////////
        $this->form->add_filedset('ganeral3', array(
            'legend' => 'Thông tin chi tiết'
        ));

        $this->form->add_filed('name', 'text', array(
            'label' => $this->__('Name'),
            'name' => 'name',
            'required' => true
        ));

        $this->form->add_filed('alias','alias',array(
            'label'=>'Alias',
            'name'=>'alias',
            'required'=>false,
        ));

        $this->form->add_filed('address', 'text', array(
            'label' => $this->__('Address'),
            'name' => 'address',
            'required' => true
        ));

        $this->form->add_filed('local_maps', 'text', array(
            'label' => $this->__('Local Maps'),
            'name' => 'local_maps',
            'required' => false
        ));

        $this->form->add_filed('open_time', 'text', array(
            'label' => $this->__('Open Time'),
            'name' => 'open_time',
            'required' => false
        ));

        $this->form->add_filed('id_careers','select',array(
            'label'=> 'Nghành nghề',
            'name'=> 'id_careers',
            'values'=>$this->Mcareers_model->get_objects_options(),
            'required' => true
        ));

        $this->form->add_filed('id_provinces','select',array(
            'label'=> 'Tỉnh thành',
            'name'=> 'id_provinces',
            'values'=>$this->Mprovinces_model->get_objects_options(),
            'required' => true
        ));

        $this->form->add_filed('about','textarea',array(
            'label'=>'About',
            'name'=>'about'
        ));

        $this->form->add_filed('contact_preson', 'text', array(
            'label' => $this->__('Contact Preson'),
            'name' => 'contact_preson',
            'required' => true
        ));

        $this->form->add_filed('contact_email', 'text', array(
            'label' => $this->__('Contact Email'),
            'name' => 'contact_email',
            'required' => true
        ));

        $this->form->add_filed('website', 'text', array(
            'label' => $this->__('Website'),
            'name' => 'website',
            'required' => false
        ));

        $this->form->add_filed('tel', 'text', array(
            'label' => $this->__('TEX'),
            'name' => 'tel',
            'required' => false
        ));

        $this->form->add_filed('fax', 'text', array(
            'label' => $this->__('FAX'),
            'name' => 'fax',
            'required' => false
        ));

        $this->form->add_filed('yahoo', 'text', array(
            'label' => $this->__('Yahoo'),
            'name' => 'yahoo',
            'required' => false
        ));

        $this->form->add_filed('skype', 'text', array(
            'label' => $this->__('Skype'),
            'name' => 'skype',
            'required' => false
        ));

        $this->form->add_filed('facebook', 'text', array(
            'label' => $this->__('Facebook'),
            'name' => 'facebook',
            'required' => false
        ));

        $this->form->add_filed('contents','editor',array(
            'label'=>'Nội dung',
            'name'=>'contents'
        ));

        $this->load->view('admin/form', array(
            'form' => $this->form->to_html()
        ));
    }
    
    function add()
    {
        $this->_forward('edit');
    }
    
    function delete()
    {
        $cids = explode(',', $this->input->get_param('id_user', false));
        foreach ($cids as $cid) {
            $this->users_model->delete($cid);
        }
        $this->_redirect(admin_url('*/udistributors/index'));
    }
    
    function status_public()
    {
        $cids = explode(',', $this->input->get_param('id_user', false));
        foreach ($cids as $cid) {
            $this->users_model->change_status($cid, 1);
        }
        $this->_redirect(admin_url('*/udistributors/index'));
    }
    
    function status_unpublic()
    {
        $cids = explode(',', $this->input->get_param('id_user', false));
        foreach ($cids as $cid) {
            $this->users_model->change_status($cid, 0);
        }
        $this->_redirect(admin_url('*/udistributors/index'));
    }
    
    function edit_post()
    {
        $id = $this->input->get_param('id', false);
        if (($data = $this->input->post())) {
            $this->upload_image($data);
            
            if ($id) {
                $this->users_model->update($data, $id, 2);
            } else {
                $this->users_model->insert($data, 2);
            }
        }
        $this->_redirect(admin_url('*/udistributors/index'));
    }
}